package povqs;

import static robocode.util.Utils.normalRelativeAngleDegrees;

import java.awt.Color;
import java.io.IOException;

import robocode.*;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.util.Utils;

public class LeaderBoot extends AbstractBoot {
	int timeScan = 0;
	ScannedRobotEvent scanEvent;
	int timeStep = 0;
	boolean desviarBala = false;
	public enum Estrategia { COORDENAR, PERSEGUIR};
	Estrategia tipEstretegia = Estrategia.COORDENAR;

	public void run() {

		setColors(Color.orange, Color.black, Color.green);
		setBulletColor(Color.red);
		
		
		
		while (true) {

			// turnRadarRightRadians(Double.MIN_NORMAL);
			if (timeScan++ > 1) {
				setTurnRadarRightRadians(Double.POSITIVE_INFINITY);

			}

			if (getOthers() == 0 && timeScan > 5) {
				onScannedRobot(scanEvent);
			}
			
			if(getOthers() < 2 || getTeammates().length < 2){
				tipEstretegia = Estrategia.PERSEGUIR;
			}

			if (pertoParede()) {

				turnRight(90);
				setBack(60);

			} else {
				
				
				/*if(!desviarBala){
					ahead(getBattleFieldWidth());
					back(getBattleFieldWidth());
				}*/
			}

			execute();
		}

	}
	
	public void onDeath(DeathEvent e) {
		try {

			broadcastMessage(new Msg(getLocationEnemy(),true));

		} catch (IOException ex) {
			ex.printStackTrace();
		} 
	}



	public void onScannedRobot(ScannedRobotEvent e) {

		setaDadosRoboADV(e);
		scanEvent = e;
		timeScan = 0;
		if (!isTeammate(e.getName())) {
			posicionaRadar(e);

			chamaBoots(enemyX, enemyY);

			if (distRoboAdv <= 50 ) {
				mira(angRobAdv + getGunCharge());
				fogo(distRoboAdv);
			}
		}
	}

	public void posicionaRadar(ScannedRobotEvent e) {
		setTurnRadarRightRadians(Utils.normalRelativeAngle(getHeadingRadians()
				+ e.getBearingRadians() - getRadarHeadingRadians()) * 2);
	}

	public void onHitRobot(HitRobotEvent e) {
		angRobAdv = e.getBearing();

		if (!isTeammate(e.getName())) {

			setTurnRight(e.getBearing());
			mira(angRobAdv + getGunCharge());
			setBack(50);

			if (distRoboAdv <= 50 && getTeammates().length <= 1) {
				setFireBullet(2);
			}
			
		} else {

			setBack(50);
			setTurnRight(angRobAdv);
		}
	}

	public void onHitWall(HitWallEvent e) {
		// angRobAdv = e.getBearing();
		setBack(50);
		setTurnLeft(90);
	}

	public void onHitByBullet(HitByBulletEvent e) {

		if (isTeammate(e.getName())) {
			try {

				sendMessage(e.getName(), new Pointer(getX(), getY(), true));

			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		/*double bulletBearing = e.getBearing(); // The direction the bullet came from
	    
	    if (Math.abs(bulletBearing) > 160 || Math.abs(bulletBearing) < 20) {
	    	
	      
	    }*/
		desviarBala = true;
	    	angRobAdv = e.getBearing();
			setTurnRight(e.getBearing());
			setAhead(60);
			setTurnRight(e.getBearing()+50);
			setBack(90);
	     	
	    desviarBala = false;
		/*valor = 500;
		
		desviar(e.getBearing());
		vai = !vai;*/

	}

	public void onSkippedTurn(SkippedTurnEvent e) {

		setAhead(10);

		if (direita) {

			setTurnLeft(90 - angRobAdv);
			direita = false;

		} else {

			setTurnRight(90 - angRobAdv);
			direita = true;
		}

	}

	public void onBulletHit(BulletHitEvent e) {

		// ahead(distRoboAdv);
		// fireBullet(3);
	}

	public void onWin(WinEvent e) {
		setColors(Color.black, Color.white, Color.red);
		dancinha();
	}

	public void chamaBoots(double enemyX, double enemyY) {
		try {

			broadcastMessage(new Pointer(enemyX, enemyY));

		} catch (IOException ex) {
			ex.printStackTrace();
		}

	}

}
